
package com.thinkgem.jeesite.common.pojo.auth.access.process;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.thinkgem.jeesite.common.pojo.auth.access.annotation.AccessNeeded;
import com.thinkgem.jeesite.common.pojo.auth.access.type.Relation;
import com.thinkgem.jeesite.modules.sys.entity.Office;
import com.thinkgem.jeesite.modules.sys.entity.Role;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

/** 
 * need访问处理器(类似retain),满足目标的项才能通过访问,否则都被拦截
 *
 */
public class NeededProcess extends AnnoProcess {

	final Logger logger = LoggerFactory.getLogger(getClass());

	/**
	 * 构造need处理器
	 * @param accessNeeded need注解
	 */
	public NeededProcess(AccessNeeded accessNeeded) {
		logger.debug("process: @AccessNeeded");
		if(accessNeeded != null){
			// 根据annotation获取方法上绑定的rolelist和officelist.
			setNrlist(accessNeeded.role());      //取出annotation中的role值
			setNolist(accessNeeded.office());    //取出annotation中的office值
			setRt(accessNeeded.relation());      //取出annotation中的relation值
			setDefValue("");                     //设置用来校验的未填项默认值""
		}
    }

	public String getDefValue() {
		return defValue;
	}

	public void setDefValue(String defValue) {
		this.defValue = defValue;
	}

	public String[] getNrlist() {
		return nrlist;
	}

	public void setNrlist(String[] nrlist) {
		this.nrlist = nrlist;
	}

	public String[] getNolist() {
		return nolist;
	}

	public void setNolist(String[] nolist) {
		this.nolist = nolist;
	}

	public Relation getRt() {
		return rt;
	}

	public void setRt(Relation rt) {
		this.rt = rt;
	}

	/**
	 * 实现访问处理器的anno检查接口
	 */
	public boolean annoCheck(){
		// 获取当前用户的rolelist,officelist
		List<Role> crlist = UserUtils.getRoleList();
		List<Office> colist = UserUtils.getOfficeList();

		// 单项信息检查注解信息是否和当前用户信息匹配
		boolean nrcheck = orCheck(getNrlist(),crlist,getDefValue());
		boolean nocheck = orCheck(getNolist(),colist,getDefValue());

		// 核对两组值是否符合注解检测规则(默认'或':默认全不通过.有一项满足则通过检测,全部不满足则被拦截)
		if(relationCheck(getRt(),nrcheck,nocheck)){
			return true;// 符合检测,后续将token写入session,并跳出拦截器.
		}else{
			return false;// 不符合检测,后续直接重定向或提示无权限访问,返回false
		}
	}

	private String defValue="";

	private String[] nrlist=null;
	private String[] nolist=null;
	private Relation rt=null;

}
